package com.fm.financemanage.controller;

import com.fm.financemanage.component.TokenComponent;
import com.fm.financemanage.constant.ResponseEnum;
import com.fm.financemanage.exception.ParameterException;
import com.fm.financemanage.model.po.User;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.accountant.AccountantListVo;
import com.fm.financemanage.model.vo.accountant.AccountantOperatorVo;
import com.fm.financemanage.model.vo.user.UserListVo;
import com.fm.financemanage.response.Callback;
import com.fm.financemanage.response.JsonData;
import com.fm.financemanage.service.AccountantService;
import com.fm.financemanage.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/user")
@Api(tags = "用户信息")
@RestController
@Validated
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/")
    @ApiOperation("查询用户列表")
    public JsonData<PageVo<List<UserListVo>>> getList(HttpServletRequest request, HttpServletResponse response,
                                                      @ApiParam(name = "name", value = "名称,", required = true)
                                                      @RequestParam(name = "name", required = false) String name,
                                                      @ApiParam(name = "state", value = "0 禁用 1启用", required = true)
                                                      @RequestParam(name = "state", required = false) Integer state,
                                                      @ApiParam(name = "page", value = "页码", required = true)
                                                      @RequestParam(name = "page", required = false) Integer page,
                                                      @ApiParam(name = "limit", value = "条数", required = true)
                                                      @RequestParam(name = "limit", required = false) Integer limit
    ) {
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), userService.getList(name, state, page, limit));
    }


    @PostMapping("/")
    @ApiOperation("修改或新增用户")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @RequestBody User user, BindingResult bindingResult
    ) {
        userService.operator(user);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }


    @PostMapping("/reset/password")
    @ApiOperation("重置密码")
    public JsonData resetPassword(HttpServletRequest request, HttpServletResponse response,
                                  @ApiParam(name = "password", value = "密码", required = true)
                                  @RequestParam(name = "password", required = false) String password,
                                  @ApiParam(name = "userId", value = "用户id", required = true)
                                  @RequestParam(name = "userId", required = false) Integer userId
    ) {
        userService.resetPassword(userId, password);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }

    @DeleteMapping("/{uuid}")
    @ApiOperation("删除用户")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable("uuid") String uuid
    ) {
        userService.removeByUuid(uuid);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }
}
